import * as THREE from 'three';
import translations from '../locale';

export default class Preloader {
    constructor(_path, _manifesto, _parent) {
        console.log('预加载模块类')
        this.path = _path;
        this.manifesto = _manifesto;
        this.parent = _parent;
        this.assetCount = 0;
        this.assetTotal = _manifesto.length;
        this.loaderText = new THREE.TextureLoader();
        this.loaderMesh = new THREE.ObjectLoader();
        this.loaderCube = new THREE.CubeTextureLoader();
        this.cargo = {};
        this.container = document.getElementById("preloader");
        this.progBar = document.getElementById("preProg");
        this.detailBox = document.getElementById("preDetail");
    }
    start () {
        this.container.className = "visible";
        this.detailBox.innerHTML = translations["LOADING_ASSETS"];
        let ext;
        let _this = this;
        const _loop =(i) => {
            ext = "." + _this.manifesto[i].ext;
            switch (_this.manifesto[i].type) {
                case "texture": // 质地
                    _this.loaderText.load(_this.path + "textures/" + _this.manifesto[i].name + ext, function (_obj) { this.assetAquired(_obj, this.manifesto[i].name); }.bind(_this), undefined, function (_err) { this.assetFailed(_err, this.manifesto[i].name); }.bind(_this));
                    break;
                case "mesh": // 网格
                    _this.loaderMesh.load(_this.path + "meshes/" + _this.manifesto[i].name + ".json", function (_obj) { this.assetAquired(_obj, this.manifesto[i].name); }.bind(_this), undefined, function (_err) { this.assetFailed(_err, this.manifesto[i].name); }.bind(_this));
                    break;
                case "cubetexture": // 方块质地
                    _this.loaderCube.setPath(_this.path + "textures/" + _this.manifesto[i].name + "/");
                    _this.loaderCube.load(["xp" + ext, "xn" + ext, "yp" + ext, "yn" + ext, "zp" + ext, "zn" + ext], function (_obj) { this.assetAquired(_obj, this.manifesto[i].name); }.bind(_this), undefined, function (_err) { this.assetFailed(_err, this.manifesto[i].name); }.bind(_this));
                    break;
            }
        };
        
        for (let i = 0; i < this.assetTotal; i++) {
            _loop(i);
        }
    };
    assetAquired (_obj, _name) {
        this.cargo[_name] = _obj;
        this.assetCount++;
        this.pct = this.assetCount / this.assetTotal;
        this.progBar.style.width = (this.pct * 100) + "%";
        if (this.assetCount == this.assetTotal) {
            this.complete();
        }
    };
    assetFailed (_err, _name) {
        this.assetCount++;
        this.pct = this.assetCount / this.assetTotal;
        if (this.assetCount == this.assetTotal) {
            this.complete();
        }
    };
    complete () {
        this.detailBox.innerHTML = translations["BUILDING_CAR"];
        TweenLite.delayedCall(0.5, function () {
            this.parent.preloadComplete(this.cargo);
            this.detailBox.innerHTML = translations["TAP_TO_BEGIN"];
        }.bind(this));
    };
    remove () {
        this.container.className = "";
    };
}